package com.thientan.export;

import ar.com.fdvs.dj.domain.DynamicReport;
import ar.com.fdvs.dj.domain.builders.DynamicReportBuilder;
import ar.com.fdvs.dj.domain.builders.FastReportBuilder;
import ar.com.fdvs.dj.domain.constants.Page;
import ar.com.fdvs.dj.domain.entities.columns.AbstractColumn;

public class ReportBuilder extends FastReportBuilder {

    protected boolean autoWidth = false;
    protected boolean preferLandscape = !options.getPage().isOrientationPortrait();
    private int pageWidth = 0;

    /**
     * Default constructor.
     */
    public ReportBuilder() {

        super();
    }

    @Override
    public DynamicReport build() {

        if (autoWidth) {

            // setTopMargin(20); // Uncomment for PDF
            // setBottomMargin(20); // Uncomment for PDF
            // setLeftMargin(20); // Uncomment for PDF
            // setRightMargin(20); // Uncomment for PDF

            pageWidth = options.getLeftMargin() + options.getRightMargin();
            for (int i = 0; i < report.getColumns().size(); i++) {
                pageWidth += ((AbstractColumn) report.getColumns().get(i)).getWidth();
            }

            if (preferLandscape) {
                options.getPage().setHeight(
                        Page.Page_A4_Landscape().getHeight() - (options.getTopMargin() + options.getBottomMargin()));
            } else {
                options.getPage().setHeight(
                        Page.Page_A4_Portrait().getHeight() - (options.getTopMargin() + options.getBottomMargin()));
            }

            options.getPage().setWidth(pageWidth);
        }

        return super.build();
    }

    /**
     * Set auto width, allow chain set.
     */
    public DynamicReportBuilder setAutoWidth(boolean width) {
        this.autoWidth = width;
        return this;
    }

    /**
     * This does not guarantee that acrobat reader will use landscape mode for the individual sheets of paper when
     * printing wide reports, it merely tells it it would like to if possible. Acrobat will determine if it will use
     * portrait or landscape depending on which mode allows the greater page savings. If <tt>preferLandscape</tt> is set
     * and if it (acrobat reader) determines that landscape would save pages, it will use it.
     * 
     * @param value
     * 
     * @return
     */
    public DynamicReportBuilder setPreferLandscapeOrientation(boolean value) {
        options.getPage().setOrientationPortrait(!value);
        this.preferLandscape = value;
        return this;
    }

    /**
     * Return autoWidth value.
     * 
     * @return true if report used auto width mode, otherwise false.
     */
    public boolean isAutoWidth() {
        return autoWidth;
    }

    /**
     * Return page which was calculate when auto width mode is activated.
     * 
     * @return
     */
    public int getPageWidth() {
        return pageWidth;
    }
}
